package com.mn.sso.starter.config;

import com.google.common.collect.Lists;
import com.smart.sso.client.listener.LogoutListener;
import com.smart.sso.client.session.SessionMappingStorage;
import com.smart.sso.client.session.redis.RedisSessionMappingStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.events.AbstractSessionEvent;
import org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpSessionListener;
import java.util.List;

/**
 * @author liutao
 * @date 2022年01月15日 17:31
 */
//@ConditionalOnProperty(prefix = "sso.session.redis",name = "enable",havingValue = "true")
@Configuration
public class RedisLogoutConfig {


	@Bean
	public SessionMappingStorage sessionMappingStorage() {
		return new RedisSessionMappingStorage();
	}
	/**
	 * 分布式redis方式注册单点登出Listener
	 *
	 * 注：
	 * 1.需注入RedisSessionMappingStorage
	 * 2.需要使用Spring方式注入LogoutListener，使用ServletListenerRegistrationBean方式不生效
	 */
	@Bean
	public ApplicationListener<AbstractSessionEvent> LogoutListener() {
		List<HttpSessionListener> httpSessionListeners = Lists.newArrayList();
		LogoutListener logoutListener = new LogoutListener();
		logoutListener.setSessionMappingStorage(sessionMappingStorage());
		httpSessionListeners.add(logoutListener);
		return new SessionEventHttpSessionListenerAdapter(httpSessionListeners);
	}
}
